const mongoUtil = require("./mongo_util")
const collectionName = "indexInfo"

function getAllIndexInfo(callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({}).toArray(function (err, data) { // 返回集合中所有数据
            callback(err, data)
        });
    })
}


function getDetails(callback) {
    const mmp = mongoUtil.getCollection(collectionName)
    mmp.then(collection => {
        collection.find().toArray(function (err, result) {
            console.log("resultjjjj", result);
            if (result.length) {
                callback(null, result)
                new Error()
            } else {
                callback(err ? err : {
                    reason: "获取失败"
                }, result)
            }
        })
    })
}

function createRotation(Rotation, callback) {
    var whereStr = {
        'indexId': "61820c137b26cb14db4eedfa",
        "data.data.carousels.RotationId": {
            $ne: Rotation.RotationId
        }
    }
    var updateStr = {
        $push: {
            'data.data.carousels': Rotation
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {
            callback(err, res)
        });
    })
}


function updateRotation(Rotation, callback) {
    console.log("RotationId", Rotation.RotationId);
    var whereStr = {
        // 'indexId': "61820c137b26cb14db4eedfa",
        'data.data.carousels.RotationId': Rotation.RotationId,
    }
    var updateStr = {
        $set: {
            'data.data.carousels.$': Rotation
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {
            callback(err, res)
        });
    })
}

function deleteRotation(RotationId, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({}).toArray(function (err, result) {
            result.forEach(item => {
                const carousels = item.data.data.carousels.filter(ite => {
                    return !RotationId.includes(ite.RotationId)
                })
                var whereStr = {
                    'indexId': item.indexId,
                }
                var updateStr = {
                    $set: {
                        'data.data.carousels': carousels
                    }
                };
                mongoUtil.getCollection(collectionName).then(collection => {
                    collection.updateOne(whereStr, updateStr, function (err, res) {
                        callback(err, res)
                    });
                })

            })
        })
    })
}


function addgoods(goods, callback) {
    const whereStr = {
        'indexId': "6187693ee67587a5017d0bdc",
        "data.data.hotGoodses.goodsId": {
            $ne: goods.goodsId
        }
    }
    const updateStr = {
        $push: {
            'data.data.hotGoodses': goods
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {

            callback(err, res)
        });
    })
}

function updatehotgoods(putgoods, callback) {
    var whereStr = {
        // 'indexId': "61820c137b26cb14db4eedfa",
        'data.data.hotGoodses.goodsId': putgoods.goodsId,
    }
    var updateStr = {
        $set: {
            'data.data.hotGoodses.$': putgoods
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {
            callback(err, res)
        });
    })
}


function deletehotgoods(goodsId, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({}).toArray(function (err, result) {
            result.forEach(item => {
                const hotGoodses = item.data.data.hotGoodses.filter(ite => {
                    return !goodsId.includes(ite.goodsId)
                })
                var whereStr = {
                    'indexId': item.indexId,
                }
                var updateStr = {
                    $set: {
                        'data.data.hotGoodses': hotGoodses
                    }
                };
                mongoUtil.getCollection(collectionName).then(collection => {
                    collection.updateOne(whereStr, updateStr, function (err, res) {
                        callback(err, res)
                    });
                })

            })
        })
    })
}

function addNewgoods(newgoods, callback) {
    const whereStr = {
        'indexId': "6187693ee67587a5017d0bdc",
        "data.data.newGoodses.goodsId": {
            $ne: newgoods.goodsId
        }
    }
    const updateStr = {
        $push: {
            'data.data.newGoodses': newgoods
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {

            callback(err, res)
        });
    })
}


function updatenewgoods(putnewgoods, callback) {
    var whereStr = {
        // 'indexId': "61820c137b26cb14db4eedfa",
        'data.data.newGoodses.goodsId': putnewgoods.goodsId,
    }
    var updateStr = {
        $set: {
            'data.data.newGoodses.$': putnewgoods
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {
            callback(err, res)
        });
    })
}

function deletenewgoods(newgoodsId, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({}).toArray(function (err, result) {
            result.forEach(item => {
                const newGoodses = item.data.data.newGoodses.filter(ite => {
                    return !newgoodsId.includes(ite.goodsId)
                })
                var whereStr = {
                    'indexId': item.indexId,
                }
                var updateStr = {
                    $set: {
                        'data.data.newGoodses': newGoodses
                    }
                };
                mongoUtil.getCollection(collectionName).then(collection => {
                    collection.updateOne(whereStr, updateStr, function (err, res) {
                        callback(err, res)
                    });
                })

            })
        })
    })
}


function addRemgoods(remgoods, callback) {
    const whereStr = {
        'indexId': "6187693ee67587a5017d0bdc",
        "data.data.recommendGoodses.goodsId": {
            $ne: remgoods.goodsId
        }
    }
    const updateStr = {
        $push: {
            'data.data.recommendGoodses': remgoods
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {

            callback(err, res)
        });
    })
}

function updateremgoods(putRemgoods, callback) {
    var whereStr = {
        // 'indexId': "61820c137b26cb14db4eedfa",
        'data.data.recommendGoodses.goodsId': putRemgoods.goodsId,
    }
    var updateStr = {
        $set: {
            'data.data.recommendGoodses.$': putRemgoods
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {
            callback(err, res)
        });
    })
}

function deleteRemgoods(remgoodsId, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({}).toArray(function (err, result) {
            result.forEach(item => {
                const recommendGoodses = item.data.data.recommendGoodses.filter(ite => {
                    return !remgoodsId.includes(ite.goodsId)
                })
                var whereStr = {
                    'indexId': item.indexId,
                }
                var updateStr = {
                    $set: {
                        'data.data.recommendGoodses': recommendGoodses
                    }
                };
                mongoUtil.getCollection(collectionName).then(collection => {
                    collection.updateOne(whereStr, updateStr, function (err, res) {
                        callback(err, res)
                    });
                })

            })
        })
    })
}
module.exports = {
    getAllIndexInfo,
    getDetails,
    createRotation,
    updateRotation,
    deleteRotation,
    addgoods,
    updatehotgoods,
    deletehotgoods,
    addNewgoods,
    updatenewgoods,
    deletenewgoods,
    addRemgoods,
    updateremgoods,
    deleteRemgoods
}